package com.example.util;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Component;

/**
 * <p>
 *   邮件工具类
 * </p>
 * @author 34448
 */
@Component
public class EmailUtil {

    @Autowired
    private JavaMailSender mailSender;

    /**
     * 发送邮件
     *
     * @param to 收件人邮箱
     * @param subject 主题
     * @param text 正文
     * @return
     */
    public boolean sendEmail(String to, String subject, String text) {
        SimpleMailMessage message = new SimpleMailMessage();
        message.setTo(to);
        message.setSubject(subject);
        message.setText(text);

        try {
            mailSender.send(message);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }
}